#!../../cgi-bin/parser3.cgi
@setup[]
  $request:charset[utf-8]
  $response:charset[utf-8]

  $SQL.connect-string[mysql://user@localhost/base?timeout=600]

  ^pfConsole:disableColors[]

@main[]
  ^setup[]

  ^use[pf/management/pfCommandsManager.p]
  $manager[^pfCommandsManager::create[
    $.args[
     $.argv[$request:argv]
    ]
  ]]

  ^if($request:argv == 1){
    ^usage[] 
    ^manager.usage[]
  }{
    ^manager.process[$request:argv]    
   }  

@usage[]
  ^pfConsole:writeln[Usage: ^file:basename[$request:argv.0] command[/subcommand] [options]]

@auto[filespec][lParserDir]
  $confdir[^file:dirname[$filespec]] 
# Назначаем директорию со скриптом как рут для поиска
  $request:document-root[$confdir]
             
  $parserlibsdir[/../../cgi-bin/lib]
#  $parserlibsdir[.]

  $charsetsdir[$parserlibsdir/charsets]
  $sqldriversdir[$parserlibsdir/lib]

  $CHARSETS[
#    $.koi8-r[$charsetsdir/koi8-r.cfg]
    $.iso-8859-1[$charsetsdir/windows-1250.cfg]
    $.windows-1251[$charsetsdir/windows-1251.cfg]
  ]

$SQL[
   $.drivers[^table::create{protocol	driver	client
mysql	$sqldriversdir/libparser3mysql.so	libmysqlclient.so
}]
]

$CLASS_PATH[^table::create{path
/../../classes/
/../models/
/../helpers/
/../controllers/
/../management/
}]

^use[pf/io/pfConsole.p]  
^use[pf/debug/pfRuntime.p]
#$pfRuntime:memoryLimit(16000)

@postprocess[aBody]
  ^if(!$pfConsole:isEmpty){
    $result[$pfConsole:stdout]
  }{
     $result[$aBody]
   }

@unhandled_exception[exception;stack]
 ^pfConsole:writeln[]

 ^pfConsole:setColor[white;red]
 ^pfConsole:writeln[Unhandled Exception^if(def $exception.type){ ($exception.type)}]
 ^pfConsole:writeln[]

 ^pfConsole:writeln[  Source: $exception.source]
 ^pfConsole:writeln[ Comment: $exception.comment]

  ^if(def $exception.file){
    ^pfConsole:writeln[   Where: $exception.file (${exception.lineno}:$exception.colno)]
  }

  ^if($stack){
    ^pfConsole:writeln[          ---------------------------------------------------------------------]
    ^pfConsole:writeln[]
    ^pfConsole:setColor[white;brown]
    ^pfConsole:writeln[Stack trace:]
    ^stack.menu{
      ^pfConsole:writeln[$stack.name | $stack.file^(${stack.lineno}:$stack.colno^)]
    }
  }

 ^pfConsole:writeln[]

 $result[$pfConsole:stdout]
}   
